//
//  BN_MainTabbarController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-7-15.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

// AppDelegate
#import "AppDelegate.h"

// UIViewController
#import "BN_MainTabbarController.h"
#import "BN_DynamicViewController.h"
#import "BN_NoticeViewController.h"
#import "BN_ChatViewController.h"
#import "BN_NurserySchoolViewController.h"
#import "BN_MineViewController.h"

// UIView
#import "BN_LoginView.h"

@interface BN_MainTabbarController ()

@end

@implementation BN_MainTabbarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.hidden = YES;
    self.view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [self navigationType];
    [self toFirstLogin];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toFirstLogin) name:@"K_APP_LOGIN" object:nil];
}

- (void)toFirstLogin {
    self.viewControllers = nil;
    if(![[NSUserDefaults standardUserDefaults] boolForKey:FIRSTRUN]){
        BN_LoginView *loginView = [[[NSBundle mainBundle]loadNibNamed:@"BN_LoginView" owner:nil options:nil]objectAtIndex:0];
        [loginView callBackLoginFinished:^(BOOL isFinished) {
            if (isFinished) {
                [loginView removeFromSuperview];
                BN_UserInfo *userInfo_ = [BN_UserHelper unArchiverAccountInfo];
                NSInteger userType = [userInfo_.userType integerValue];
                switch (userType) {
                    case ENUM_ROLE_LEADER:
                        [self loadViewController];
                        break;
                    case ENUM_ROLE_CLASS_TEACHER:
                        [self loadViewController];
                        break;
                    case ENUM_ROLE_PARENT:
                        if(![[NSUserDefaults standardUserDefaults] boolForKey:USERDEFAULTS_REMEMBERBIRTHDAY]) {
                            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:USERDEFAULTS_REMEMBERBIRTHDAY];
                            BN_BirthdayView *birthView = [[[NSBundle mainBundle]loadNibNamed:@"BN_BirthdayView" owner:nil options:nil]objectAtIndex:0];
                            [birthView callBackEnterApp:^(BOOL isEnter) {
                                if (isEnter) {
                                    [birthView removeFromSuperview];
                                    [self loadViewController];
                                }
                            }];
                            [self.view addSubview:birthView];
                        }else {
                            [self loadViewController];
                        }
                        break;
                    default:
                        break;
                }
            }
        }];
        [self.view addSubview:loginView];
    }else {
        [self loadViewController];
    }
}

#pragma mark
#pragma mark 动画
- (void)donghuaX:(UIView *)animationView {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"fade";
    // @"cube" @"moveIn" @"reveal" @"fade"(default) @"pageCurl" @"pageUnCurl" @"suckEffect" @"rippleEffect" @"oglFlip"
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [animationView.layer addAnimation:transition forKey:nil];
    [self.view.layer addAnimation:transition forKey:@"animation"];
}

#pragma mark
#pragma mark navigationType
- (void)navigationType {
    AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
    // NavgationBar Tabbar 透明度处理
    [[UITabBar appearance]setBackgroundImage:[[UIImage alloc]init]];
    [[UITabBar appearance]setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.8]];
    
    UIImage *navBarImg = [UIImage imageNamed:@"daohanglan"];
    navBarImg = [navBarImg resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 44, 10)];
    
    [[UINavigationBar appearance]setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
    
    // NavgationBar 文字白色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [UIColor whiteColor], UITextAttributeTextColor,
                                                          [UIColor darkGrayColor], UITextAttributeTextShadowColor,
                                                          nil]];
    
    
    if (MAIN_VERSION < 7) {
        appdelegate.window.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.1];
        [[UINavigationBar appearance]setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
        for (UIView *subview in self.view.subviews) {
            NSLog(@"subview: %@", subview);
            if ([subview isKindOfClass:NSClassFromString(@"UITransitionView")]) {
                CGRect appRect = [[UIScreen mainScreen]applicationFrame];
                subview.frame = CGRectMake(0, 0, appRect.size.width, appRect.size.height + 20);
                break;
                
            }
        }
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];
        self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
        self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
        [[UINavigationBar appearance]setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];
        
    }else{
        [[UINavigationBar appearance]setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];
    }
}

#pragma mark
#pragma mark 加载 ViewController
- (void)loadViewController {
    
    BN_DynamicViewController *dynamicVC = [[BN_DynamicViewController alloc]init];
    dynamicVC.title = @"动态";
    [self toDealWith:dynamicVC];
    UINavigationController *dynamicNav = [[UINavigationController alloc]initWithRootViewController:dynamicVC];
    
    BN_NoticeViewController *noticeVC = [[BN_NoticeViewController alloc]init];
    noticeVC.title = @"通知";
    [self toDealWith:noticeVC];
    UINavigationController *noticeNav = [[UINavigationController alloc]initWithRootViewController:noticeVC];
    
    BN_ChatViewController *chatVC = [[BN_ChatViewController alloc]init];
    chatVC.title = @"聊聊";
    [self toDealWith:chatVC];
    UINavigationController *chatNav = [[UINavigationController alloc]initWithRootViewController:chatVC];
    
    BN_NurserySchoolViewController *nurserySchoolVC = [[BN_NurserySchoolViewController alloc]init];
    nurserySchoolVC.title = @"幼儿园";
    [self toDealWith:nurserySchoolVC];
    UINavigationController *nurserySchoolNav = [[UINavigationController alloc]initWithRootViewController:nurserySchoolVC];
    
    BN_MineViewController *mineVC = [[BN_MineViewController alloc]init];
    mineVC.title = @"我的";
    [self toDealWith:mineVC];
    UINavigationController *mineNav = [[UINavigationController alloc]initWithRootViewController:mineVC];
    
    _viewCtrls = @[dynamicNav,noticeNav,chatNav,nurserySchoolNav,mineNav];
    self.viewControllers = _viewCtrls;
}

#pragma mark
#pragma mark Tabbar 添加图片文字信息
- (void)toDealWith:(UIViewController *)viewcontroller {
    
    UIImage *normalState = nil;
    UIImage *selectState = nil;
    
    if ([viewcontroller.title isEqualToString:@"动态"]) {
        normalState = [UIImage imageNamed:@"dongtai_normal"];
        selectState = [UIImage imageNamed:@"dongtai_selected"];
        // 添加标题
        [viewcontroller.tabBarItem setTitle:@"动态"];
    }
    
    if ([viewcontroller.title isEqualToString:@"通知"]) {
        normalState = [UIImage imageNamed:@"tongzhi_normal"];
        selectState = [UIImage imageNamed:@"tongzhi_selected"];
        // 添加标题
        [viewcontroller.tabBarItem setTitle:@"通知"];
    }
    
    if ([viewcontroller.title isEqualToString:@"聊聊"]) {
        normalState = [UIImage imageNamed:@"liaoliao_normal"];
        selectState = [UIImage imageNamed:@"liaoliao_selected"];
        // 添加标题
        [viewcontroller.tabBarItem setTitle:@"聊聊"];
    }
    if ([viewcontroller.title isEqualToString:@"幼儿园"]) {
        normalState = [UIImage imageNamed:@"youeryuan_normal"];
        selectState = [UIImage imageNamed:@"youeryuan_selected"];
        // 添加标题
        [viewcontroller.tabBarItem setTitle:@"幼儿园"];
    }
    if ([viewcontroller.title isEqualToString:@"我的"
        ]) {
        normalState = [UIImage imageNamed:@"wode_normal"];
        selectState = [UIImage imageNamed:@"wode_selected"];
        // 添加标题
        [viewcontroller.tabBarItem setTitle:@"我的"];
    }
    
    // 设置标题高亮颜色
    UIColor *color = APP_OBJECT_GREEN_COLOR;
    [viewcontroller.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:color, nil] forKeys:[NSArray arrayWithObjects:UITextAttributeTextColor, nil]] forState:UIControlStateHighlighted];
    // 设置tabbarItem 选中或未选中状态
    [viewcontroller.tabBarItem setFinishedSelectedImage:selectState withFinishedUnselectedImage:normalState];
    [[UITabBar appearance]setBackgroundImage:[[UIImage alloc]init]];
    [[UITabBar appearance]setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.8]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
